package singleThreaded;
import java.net.URL;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;

	
	

public class PageAnalyzerSingle {
	private String my_keywords[];
	private PageParserSingle my_parser;
	private List<URL>my_sources;
	private final double BILLION = 1000000000;
	
	public PageAnalyzerSingle(PageParserSingle the_parser, List<URL> the_sources) {
		
		my_parser = the_parser;
		my_sources = the_sources;
	}
	/**
	 * initialize the keywords. store them all in the list.
	 */
	public void initializeKeyword(String the_keywords){
		my_keywords = the_keywords.split(" ");
	}
	
	/**
	 * Analyzing Data for the output.
	 */
	public StringBuilder getAnalyzingData() {
		DecimalFormat df = new DecimalFormat("#.##");
		StringBuilder result = new StringBuilder();
		String newline = "\n";
		result.append("Pages Retrieve : " + 
				getTotalPagesCrawl() + newline);
		result.append("Average words per page: " +
				df.format(getAverageWordPerPage())+ newline);
		result.append("Average URLs per page: " +
				df.format(getAverageURLsPerPage())+ newline);
		result.append("keywords" + "\t" + "Avg. hits" +
				"\t" + "total hits" + newline);
		int [] total_count = keywordsConcurrence();
		double[] average_count = new double[total_count.length];
		for (int l = 0; l < my_keywords.length; l++) {
			average_count[l] = (double) total_count[l] / (double) my_parser.getContentsForAllSources().size();
			result.append(my_keywords[l] + "\t" + df.format(average_count[l]) + "\t" +
					total_count[l] + newline);
		}
		DecimalFormat dform = new DecimalFormat("#.####");
		result.append("Average time per page: ");
		result.append(dform.format(getAverageParseTime()) + " sec" + newline);
		return result;
	}
	
	/**
	 * calculate the keyword match with for all pages.
	 */
	private int[] keywordsConcurrence(){
		int total_count[] = new int[my_keywords.length];
		for (int i = 0; i < my_parser.getContentsForAllSources().size(); i++) {
			for (int j = 0; j < my_parser.getContentsForAllSources().get(i).size(); j++) {
				for(int k = 0; k < my_keywords.length; k++){	
					if (my_keywords[k].equalsIgnoreCase((my_parser.getContentsForAllSources().get(i).get(j)))) {
						total_count[k]++;
					}
				}
			}
		}
		return total_count;
		
	}
	/**
	 * get the average words per page.
	 */
	private double getAverageWordPerPage(){
			double count_all_pages = 0;
			int i = 0;
			while (i < my_parser.getContentsForAllSources().size()) {
				count_all_pages += my_parser.getContentsForAllSources().get(i).size();
				i++;
			}
			return count_all_pages / my_sources.size();
	}
	
	/** 
	 * get the number of pages retrieves. 
	 */
	private int getTotalPagesCrawl() {
		return my_sources.size();
	}
	
	/**
	 * get the average urls per page.
	 */
	private double getAverageURLsPerPage() {
		return (double) my_parser.getTotalLinks() / my_sources.size();	
	}
	
	/**
	 * get the average parse time.
	 */
	private double getAverageParseTime() {
		return my_parser.getTotalParseTime() /my_sources.size()/BILLION;
	}
}
